<?php
	define('CONFIG_FILE',true);
	include '../config.php';
	
	define('DB_FILE',true);
	require_once '../Tool/DB/SqlDBManager.class.php';
	
	session_start();
	if (empty($_SESSION["userType"])) {
		$url="../Index.php";
		include_once '../Tool/Common/Redirect.php';
		exit();
	}

	if($_SESSION["userType"]=="Therapy Assistant"){
        $url="../Index.php";
		include_once '../Tool/Common/Redirect.php';
		exit();
    }
    
	$page = isset($_POST['page']) ? intval($_POST['page']) : 1;
	$rows = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
	
	$offset = ($page-1)*$rows;
	$result = array();

	$sqlDBManager = new SqlDBManager();
	
	$sql = "select count(*) from user";
	$res=$sqlDBManager->queryRow($sql);
	$result["total"] = $res[0];
	
	//Filter the $searchid
	$searchid=addslashes($searchid);
	$searchid=str_replace("%", "\%", $searchid);
	$searchid=str_replace("_", "\_", $searchid);
	
	$sql ="select * from user where 
	Username like ? 
	or UserType like ? 
	or StaffName like ? 
	or CentreId like ? 
	or ContactNumber like ? 
	or Email like ? 
	limit $offset,$rows";
	
	$parameters = array("%".$searchid."%","%".$searchid."%","%".$searchid."%","%".$searchid."%","%".$searchid."%","%".$searchid."%");
	$res=$sqlDBManager->queryRows($sql,$parameters);
	$items = array();
	if(!empty($res)){
		for($i=0;$i<count($res);$i++){
			array_push($items, $res[$i]);
		}	
	}else{
		array_push($items, NULL);
	}
	//close connection
  	$sqlDBManager->close_connect();

	$result["rows"] = $items;
	echo json_encode($result);
?>